package think.in.java.concurrency.PractiseOneMoreTime;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Modify Exercise 2 so that the task is a Callable that sums the values of all the Fibonacci numbers. Create several tasks and display the results.
 */
public class Exec5ForCallable implements Callable<List<Integer>> {
    private Integer iteratorTimes=0;
    public Exec5ForCallable(Integer iteratorTimes){
        this.iteratorTimes = iteratorTimes;
    }

    @Override
    public List<Integer> call() throws Exception {
        IteratorFibonacci iteratorFibonacci = new IteratorFibonacci(iteratorTimes);
        List<Integer> resultList = new ArrayList<Integer>();
        for(Integer integer : iteratorFibonacci)
            resultList.add(integer);
        return resultList;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        List<Future<List<Integer>>> futureList = new ArrayList<Future<List<Integer>>>();
        for(int i=0; i < 3; i++){
            futureList.add(executorService.submit(new Exec5ForCallable(20)));
        }
        for(Future<List<Integer>> future : futureList){
            List<Integer> resultList = future.get();
            for(Integer result : resultList){
                System.out.println(result);
            }
        }
        executorService.shutdown();
    }


}
